﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.IO;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BaoJianSoft.Common;
using BaoJianSoft.RapidWeb.Properties;

namespace BaoJianSoft.RapidWeb
{
    /// <summary>
    /// Http handler for file uploading
    /// </summary>
    public class FileUploadHttpHandler : IHttpHandler
    {
        /// <summary>
        /// Gets a value indicating whether another request can use the System.Web.IHttpHandler instance.
        /// </summary>
        public virtual bool IsReusable { get { return true; } }

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the System.Web.IHttpHandler interface.
        /// </summary>
        /// <param name="context"></param>
        public virtual void ProcessRequest(HttpContext context)
        {
            if (string.IsNullOrEmpty(HttpContext.Current.Request["Feature"]))
				throw new HttpException(500, Resources.Ctrl_QueryStringFeatureNotSpecified);

            if (string.IsNullOrEmpty(HttpContext.Current.Request["UploadingIdentifier"]))
				throw new HttpException(500, Resources.Ctrl_QueryStringUploadingIdIdentifierNotSpecified);

            string tempFilesPath = WebUtility.ResolveFlashUploadFilesPath(HttpContext.Current.Request["Feature"], HttpContext.Current.Request["UploadingIdentifier"]);
            if (!Directory.Exists(tempFilesPath))
            {
                Directory.CreateDirectory(tempFilesPath);
            }

            if (context.Request.Files.Count > 0)
            {
                // Loop through all the uploaded files
                for (int index = 0; index < context.Request.Files.Count; index++)
                {
                    HttpPostedFile postedFile = context.Request.Files[index];
                    string tempFilePath = Path.Combine(tempFilesPath, postedFile.FileName);
                    postedFile.SaveAs(tempFilePath);
                }
            }

            // Used as a fix for a bug in mac flash player that makes the onComplete event not fire
            HttpContext.Current.Response.Write(" ");
        }
    }
}

